package com.amoyt.project.enums;

import com.amoyt.project.domain.vo.PermissionVo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Date: 2025/8/19
 * @Time: 14:50
 * @Author: TQ_QT
 * @Description: 管理员等级枚举
 */
@NoArgsConstructor
@AllArgsConstructor
public enum AdminLevelEnum {
    //    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin','village_admin')")
    SUPER_ADMIN(1,"super_admin", "超级管理员","national"),
    PROV_ADMIN(2,"prov_admin", "省级干部","prov"),
    CITY_ADMIN(3,"city_admin", "市级干部","city"),
    REGION_ADMIN(4,"region_admin", "区县级干部","region"),
    TOWN_ADMIN(5,"town_admin", "镇/乡干部","town"),
    VILLAGE_ADMIN(6,"village_admin", "村干部","village");

    private Integer level;
    private String role;
    private String desc;
    private String unit; //单位级别

    /**
     * 获取当前角色的下级角色的role字符串列表
     * @param currRole 当前角色
     * @return
     */
    public static List<String> getLowerLevelRole(String currRole){
        //获取当前枚举
        AdminLevelEnum currRoleEnum = getByRole(currRole);
        //遍历当前枚举 比较level，找到比之更大的角色,但是
        return Arrays.stream(AdminLevelEnum.values()).filter(adminLevelEnum -> adminLevelEnum.getLevel() > currRoleEnum.getLevel()).map(AdminLevelEnum::getRole).toList();
    }

    /**
     * 根据角色获取枚举
     * @param role
     * @return
     */
    private static AdminLevelEnum getByRole(String role){
        for (AdminLevelEnum value : AdminLevelEnum.values()) {
            if (value.getRole().equals(role)){
                return value;
            }
        }
        return null;
    }


    /**
     * 是否是管理员
     * @param roleKey
     * @return
     */
    public static boolean isAdmin(String roleKey){
        return getByRole(roleKey) != null;
    }

    /**
     * 是否是下级管理员
     * @param currRole 当前登录的角色
     * @param roleKey 传入的角色
     * @return
     */
    public static boolean isLowerAdmin(String currRole, String roleKey){
        //判断
        return getByRole(currRole) != null && getByRole(roleKey) != null && getByRole(currRole).getLevel() < getByRole(roleKey).getLevel();
    }

    /**
     * 获取当前角色的下级角色
     * @param roleKey
     */
    public static List<PermissionVo> listLowerRole(String roleKey) {
        //查找
        AdminLevelEnum currRole = getByRole(roleKey);
        List<PermissionVo> ans = new ArrayList<>();

        //循环
        for (AdminLevelEnum role : AdminLevelEnum.values()) {
            if(role.level > currRole.level){
                ans.add(new PermissionVo(role.role, role.desc));
            }
        }
        return ans;
    }

    /**
     * 判断当前角色是否是当前单位
     * @param roleKey
     * @param unit
     * @return
     */
    public static boolean isCurrentUnit(String roleKey,String unit){
        AdminLevelEnum role = getByRole(roleKey);
        return role.unit.equals(unit);
    }


    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}
